package javango.forms.fields;

import java.math.BigDecimal;
import java.util.Date;

import javango.forms.widgets.WidgetFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Inject;
import com.google.inject.Injector;

public class FieldFactoryImpl implements FieldFactory {

	private final static Log log = LogFactory.getLog(FieldFactoryImpl.class);
	
	protected Injector injector;
	
	@Inject
	public FieldFactoryImpl(Injector injector) {
		super();
		this.injector = injector;
	}

	public <T> T newField(Class<T> clazz) {
		return injector.getInstance(clazz);
	}
	
	public Field newFieldForClass(Class<?> type) {
		if (type == String.class) {
			return newField(CharField.class);
		} else if (type == Long.class) {
			return newField(LongField.class);
		} else if (type == Double.class) {
			return newField(DoubleField.class);
		} else if (type == Integer.class) {
			return newField(IntegerField.class);
		} else if (type == BigDecimal.class) {
			return newField(BigDecimalField.class);
		} else if (type == Boolean.class) {
			return newField(BooleanField.class);
		} else if (type == Date.class) {
			return newField(DateField.class);
		} else {
			log.info("Unknown type :" + type.getName());
			return newField(CharField.class);
		}
	}

	public WidgetFactory getWidgetFactory() {
		return injector.getInstance(WidgetFactory.class);
	}
	
	
}
